this.name			= "oreProcessor";
this.author			= "Eric";
this.copyright		= "Creative Commons: attribution, non-commercial, sharealike.";
this.description	= "Ore processor main";
this.version		= "1.55";

this.message = new Array()
this.type = new Array()
this.quantity = new Array()
this.count = new Array()

this.startUp = function()
{
	 this.oreTimer = new Timer(this, this.processOre, 5, 0.5)
	 this.oreTimer.stop()
	 
	 this.mySound = new SoundSource;
	 this.mySound.sound = "op_process.ogg";
	 this.mySound.loop = true
}

this.playerWillEnterWitchspace = function() {
    if (this.oreTimer) {
		this.oreTimer.stop();
		delete this.oreTimer;
	}
};

// addOre is only used by splinters scooped up by the player so there is no need for player tests in this script.
this.addOre = function(message, type, quantity)
{
	var pos = this.message.length
	this.message[pos]=message;
	this.type[pos]=type;
	this.quantity[pos]=quantity;
	this.count[pos]=10;
	if(this.oreTimer) {
		this.oreTimer.start();
	} else {
		this.oreTimer = new Timer(this, this.processOre, 5, 0.5);
	}
	this.mySound.play()
}

this.processOre = function()
{
	if(this.count[0] > 0 && player.ship.energy > 64)
	{
		this.count[0]--
		player.ship.energy -= 6.4
	}
	else
	{
		if(this.toggle) this.toggle=false; else this.toggle = true; // to avoid identical message supression.
		if(player.ship.energy > 64)
		{
			if(this.canAwardCargo(this.type[0], this.quantity[0]))
            {
                player.consoleMessage(expandDescription(this.message[0])+(this.toggle?"":" "));
                this.awardCargo(this.type[0], this.quantity[0])
            }
            else
            {
                player.consoleMessage("Could not store processed goods, dumping...");
                player.ship.ejectItem("splinter");
            }
		}
		else
		{
			player.consoleMessage("Energy failure, no extraction!! 1 ton ore");
            if(this.canAwardCargo
            ("Minerals", 1))
            {
                this.awardCargo("Minerals", 1)
            }
            else
            {
                player.consoleMessage("Could not store minerals, dumping...");
                player.ship.ejectItem("splinter");
            }
		}
		this.message.splice(0,1)
		this.type.splice(0,1)
		this.quantity.splice(0,1)
		this.count.splice(0,1)
		if(this.message.length==0) {
			if(this.oreTimer) {
				this.oreTimer.stop(); 
			}
			this.mySound.stop();
		} else if(!this.canAwardCargo(this.type[0], this.quantity[0])) this.count[0] = 2;
	}
}

this.canAwardCargo = function(type, quantity)
{
    if (0 < oolite.compareVersion("1.74")) 
    {
        if(player.ship.canAwardCargo(type, quantity)) return true; else return false;
    }
    else
    {
        var cargo = type.toLowerCase()
        if (type = "gold" && Manifest["gold"] < 1E3-quantity) return true;
        if (type = "platinum" && Manifest["platinum"] < 1E3-quantity) return true;
        if (type = "gem-stones" && Manifest["gem-stones"] < 1E6-quantity) return true;
        if(player.ship.cargoSpaceAvailable > quantity) return true; else return false;
    }

}

this.awardCargo = function(type, quantity)
{
    if (0 < oolite.compareVersion("1.74")) player.ship.awardCargo(type, quantity);
    else Manifest[type.toLowerCase()] += quantity;
}